package com.xbongbong.pro.rabbitmq.producer.impl;

import com.xbongbong.paas.enums.EnableEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.api.pojo.ApiWebHookRabbitMqDTO;
import com.xbongbong.pro.enums.ApiCallBackEnum;
import com.xbongbong.pro.enums.errorcodes.MessageErrorCodeEnum;
import com.xbongbong.pro.rabbitmq.binders.ApiWebHookProducerBinder;
import com.xbongbong.pro.rabbitmq.producer.ApiWebHookProducer;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * api webhook 生产者
 * @author 魏荣杰
 * @date 2019/10/10 20:32
 * @since v4.3.2.3
 * @version v4.3.2.3
 */
@Service("apiWebHookProducer")
@EnableBinding({ApiWebHookProducerBinder.class})
public class ApiWebHookProducerImpl implements ApiWebHookProducer {
    private static final Logger LOG = LoggerFactory.getLogger(ApiWebHookProducerImpl.class);
    @Resource
    private ApiWebHookProducerBinder apiWebHookProducerBinder;

    @Override
    public void sendMessage(ApiWebHookRabbitMqDTO apiWebHookRabbitMqDTO, XbbRefTypeEnum xbbRefTypeEnum) throws XbbException {
        if (Objects.nonNull(apiWebHookRabbitMqDTO) && Objects.nonNull(xbbRefTypeEnum)) {
            ApiCallBackEnum apiCallBackEnum = ApiCallBackEnum.getByBusiness(xbbRefTypeEnum);
            if (Objects.isNull(apiCallBackEnum) || apiCallBackEnum.getEnable().equals(EnableEnum.CLOSE.getCode())) {
                throw new XbbException(MessageErrorCodeEnum.API_ERROR_380001);
            }
            apiWebHookRabbitMqDTO.setXbbRefType(xbbRefTypeEnum);
            MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
            messageHeaderAccessor.setHeader("x-delay", 10000);
            MessageBuilder messageBuilder = MessageBuilder.withPayload(apiWebHookRabbitMqDTO).setHeaders(messageHeaderAccessor);

            switch (xbbRefTypeEnum) {

                case CUSTOMER:
                    apiWebHookProducerBinder.apiCustomerOutput().send(messageBuilder.build());
                    break;
                case CONTRACT:
                    apiWebHookProducerBinder.apiContractOutput().send(messageBuilder.build());
                    break;
                case SALES_OPPORTUNITY:
                    apiWebHookProducerBinder.apiOpportunityOutput().send(messageBuilder.build());
                    break;
                case SYSTEM:
                    apiWebHookProducerBinder.apiCustomFormOutput().send(messageBuilder.build());
                    break;
                case CLUE:
                    apiWebHookProducerBinder.apiClueOutput().send(messageBuilder.build());
                    break;
                case CUSTOMER_COMMUNICATE:
                    apiWebHookProducerBinder.apiCustomerCommunicateOutput().send(messageBuilder.build());
                    break;
                default:
                    apiWebHookProducerBinder.apiCommonOutput().send(messageBuilder.build());
            }
        }
    }


}
